package gui;
import javax.swing.*;
import javax.swing.tree.*;

import model.TreeItem;

import java.awt.Color;
import java.awt.Component;
 
public class IconTreeRenderer extends DefaultTreeCellRenderer {
 
	private static final long serialVersionUID = 1L;
  private TreeItem treeItem;
  private Icon leafIcon = new ImageIcon( "img/tree/Text.png" );
  private Icon highPrio = new ImageIcon("img/tree/Alert.png");
  private Icon midPrio = new ImageIcon("img/tree/Warning.png");
  private Icon lowPrio = new ImageIcon("img/tree/Back.png");
  private Icon close = new ImageIcon("img/tree//Black tag.png");
  private Icon open= new ImageIcon("img/tree/Blue tag.png");
  private Icon done = new ImageIcon("img/tree/Yes.png");
  
  public IconTreeRenderer() 
  {
    
    super();
  }
  
  public Icon getLeafIcon() 
  {
	if(treeItem != null){
	if (treeItem.isDone())
		return done;
    if (treeItem.getPrio() == 2)
    	return highPrio;
    if (treeItem.getPrio() == 1)
    	return midPrio;
    if (treeItem.getPrio() == 0)
    	return lowPrio;
	}
    return leafIcon;
 
  }
  public Color getTextSelectionColor(){
	  if (treeItem != null){
		  if (treeItem.isDone())
			  return Color.LIGHT_GRAY;
	  }
	  return super.getTextSelectionColor();
  }
  public Color getTextNonSelectionColor(){
	  if (treeItem != null){
		  if (treeItem.isDone())
			  return Color.GRAY;
	  }
	  return super.getTextNonSelectionColor();  }
  
  public Component getTreeCellRendererComponent( JTree tree, Object value,
                                                 boolean sel,
                                                 boolean expanded,
                                                 boolean leaf, int row,
                                                 boolean hasFocus ) {
    String stringValue = tree.convertValueToText( value, sel,
                                                  expanded, leaf, row, hasFocus );
    setOpenIcon(open);
    setClosedIcon(close);

    if (value instanceof TreeItem)
    	treeItem = (TreeItem) value;
    
    this.hasFocus = hasFocus;
    setText( stringValue );
    if ( sel )
      setForeground( getTextSelectionColor());
    else
      setForeground( getTextNonSelectionColor() );
    // There needs to be a way to specify disabled icons.
    if ( !tree.isEnabled() ) {
      setEnabled( false );
      if ( leaf ) {
        setDisabledIcon( getLeafIcon() );
      }
      else if ( expanded ) {
        setDisabledIcon( getOpenIcon() );
      }
      else {
        setDisabledIcon( getClosedIcon() );
      }
    }
    else {
      setEnabled( true );
      if ( leaf ) {
        setIcon( getLeafIcon() );
      }
      else if ( expanded ) {
        setIcon( getOpenIcon() );
      }
      else {
        setIcon( getClosedIcon() );
      }
    }
    setComponentOrientation( tree.getComponentOrientation() );
 
    selected = sel;
 
    return this;
  }
 
}